package com.hengda.museumonline.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.LoginBean;
import com.hengda.museumonline.entity.UinfoBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.rxbus.RxBus;
import com.hengda.museumonline.utils.GlideCircleTransform;
import com.hengda.museumonline.utils.ThemeChangeUtil;
import com.hengda.museumonline.utils.UinfoUtil;
import com.litesuits.common.assist.Base64;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

/**
 * @author：JFWU on 2016/9/22 13:25
 * @E-mail：jeffy12138@126.com
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {
    private Switch Switch;
    private android.widget.ImageView avatar;
    private android.widget.TextView usernicename;
    private android.widget.LinearLayout comment;
    private LinearLayout guestbook;
    private LinearLayout aboutus;
    private ImageView message;
    private LinearLayout invite;
    private LinearLayout textSize;
    private LinearLayout clearCache;
    private TextView tvSize;
    private LinearLayout footprint;
    private TextView tvsize;
    private LinearLayout clearcache;
    private ImageView ivclose;
    private LinearLayout collection;
    private TextView login;
    private LinearLayout personal;
    private ImageView sex;
    private TextView type;
    private TextView score;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        this.score = (TextView) findViewById(R.id.score);
        this.type = (TextView) findViewById(R.id.type);
        this.sex = (ImageView) findViewById(R.id.sex);
        this.personal = (LinearLayout) findViewById(R.id.personal);
        this.login = (TextView) findViewById(R.id.login);
        login.setOnClickListener(this);
        this.collection = (LinearLayout) findViewById(R.id.collection);
        collection.setOnClickListener(this);
        this.ivclose = (ImageView) findViewById(R.id.iv_close);
        ivclose.setOnClickListener(this);
        this.clearcache = (LinearLayout) findViewById(R.id.clear_cache);
        this.tvsize = (TextView) findViewById(R.id.tv_size);
        this.footprint = (LinearLayout) findViewById(R.id.footprint);
        footprint.setOnClickListener(this);
        this.aboutus = (LinearLayout) findViewById(R.id.about_us);
        aboutus.setOnClickListener(this);
        this.guestbook = (LinearLayout) findViewById(R.id.guestbook);
        guestbook.setOnClickListener(this);
        this.comment = (LinearLayout) findViewById(R.id.comment);
        comment.setOnClickListener(this);
        this.usernicename = (TextView) findViewById(R.id.user_nicename);
        this.avatar = (ImageView) findViewById(R.id.avatar);
        avatar.setOnClickListener(this);
        Switch = (android.widget.Switch) findViewById(R.id.Switch);
        Switch.setOnClickListener(this);
        message = (ImageView) findViewById(R.id.message);
        message.setOnClickListener(this);
        invite = (LinearLayout) findViewById(R.id.invite);
        invite.setOnClickListener(this);
        textSize = (LinearLayout) findViewById(R.id.textSize);
        textSize.setOnClickListener(this);
        clearCache = (LinearLayout) findViewById(R.id.clear_cache);
        clearCache.setOnClickListener(this);
        tvSize = (TextView) findViewById(R.id.tv_size);
        initData();
    }

    private void initData() {
        if (!"".equals(spUtil.getToken())) {
            personal.setVisibility(View.VISIBLE);
            login.setVisibility(View.INVISIBLE);
            UinfoUtil uinfoUtil = new UinfoUtil(SettingActivity.this, spUtil.getToken());
            uinfoUtil.setOnUinfoListener(new UinfoUtil.OnUinfoListener() {
                @Override
                public void onUinfo(UinfoBean uinfoBean) {
                    if (1 == uinfoBean.getStatus()) {
                        usernicename.setText(uinfoBean.getData().getUser_nicename());
                        Glide.with(SettingActivity.this).load(uinfoBean.getData().getAvatar()).transform(new GlideCircleTransform(SettingActivity.this)).into(avatar);
                        type.setText(uinfoBean.getData().getType());
                        score.setText(uinfoBean.getData().getScore());
                        if ("1".equals(uinfoBean.getData().getSex())) {
                            sex.setImageResource(R.drawable.setting_nan);
                        } else if ("2".equals(uinfoBean.getData().getSex())) {
                            sex.setImageResource(R.drawable.setting_nv);
                        }
                    }
                }
            });

        }

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.avatar:
                startActivity(SettingActivity.this, PersonalActivity.class);
                break;
            case R.id.login:
                startActivity(SettingActivity.this, LoginActivity.class);
                finish();
                break;
            case R.id.footprint:
                startActivity(SettingActivity.this, FootprintActivity.class);
                break;
            case R.id.message:
                startActivity(SettingActivity.this, MassageActivity.class);
                break;
            case R.id.collection:
                startActivity(SettingActivity.this, CollectionActivity.class);
                break;
            case R.id.textSize:
                new AlertDialog.Builder(SettingActivity.this)
                        .setTitle("请选择")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setSingleChoiceItems(new String[]{"大字号", "中字号", "小字号"}, 1,
                                new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface dialog, int which) {
                                        switch (which) {
                                            case 0:
                                                tvSize.setText("大字号");
                                                break;
                                            case 1:
                                                tvSize.setText("中字号");
                                                break;
                                            case 2:
                                                tvSize.setText("小字号");
                                                break;
                                        }
                                        dialog.dismiss();
                                    }
                                }
                        )
                        .setNegativeButton("取消", null)
                        .show();
                break;
            case R.id.Switch:
                if (ThemeChangeUtil.isChange) {
                    ThemeChangeUtil.isChange = false;
                } else {
                    ThemeChangeUtil.isChange = true;
                }
                recreate();//重新创建当前Activity实例
                RxBus.get().post("MainActivity", null);
                break;
            case R.id.clear_cache:

                new AlertDialog.Builder(SettingActivity.this)
                        .setTitle("确认")
                        .setMessage("确定要清除缓存吗？")
                        .setPositiveButton("是", null)
                        .setNegativeButton("否", null)
                        .show();
                break;
            case R.id.comment:
                startActivity(SettingActivity.this, CommentActivity.class);
                break;
            case R.id.invite:
                startActivity(SettingActivity.this, InviteActivity.class);
                break;
            case R.id.guestbook:
                startActivity(SettingActivity.this, GuestbookActivity.class);
                break;
            case R.id.about_us:
                startActivity(SettingActivity.this, AboutUsActivity.class);
                break;
        }
    }
}
